package com.bf.electroplating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.sewagePlantManualDetection.*;
import com.bf.electroplating.service.ISewagePlantManualDetectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 污水厂预排口手工检测数据控制器
 */
@Slf4j
@RestController
@RequestMapping("/sewagePlantManualDetection")
@Tag(name = "污水厂预排口手工检测数据", description = "污水厂预排口手工检测数据相关接口")
public class SewagePlantManualDetectionController {

    @Autowired
    private ISewagePlantManualDetectionService sewagePlantManualDetectionService;




    @PostMapping("/pageList")
    @Operation(summary = "分页查询污水厂预排口手工检测数据", description = "根据条件分页查询污水厂预排口手工检测数据")
    public ResultVO<SewagePlantManualDetectionPageVO> pageList(@RequestBody SewagePlantManualDetectionQueryDTO queryDTO) {
        try {
            SewagePlantManualDetectionPageVO result = sewagePlantManualDetectionService.pageList(queryDTO);
            return ResultVO.ok(result);
        } catch (Exception e) {
            log.error("分页查询污水厂预排口手工检测数据失败: {}", e.getMessage(), e);
            return ResultVO.fail("查询失败: " + e.getMessage());
        }
    }

    @PostMapping("/pageListForCompany")
    @Operation(summary = "分页查询污水厂预排口手工检测数据-企业", description = "根据条件分页查询污水厂预排口手工检测数据")
    public ResultVO<Page<SewagePlantManualDetectionVO>> pageListForCompany(@RequestBody SewagePlantManualDetectionQueryCompanyDTO queryDTO) {
        try {
            Page<SewagePlantManualDetectionVO> result = sewagePlantManualDetectionService.pageListForCompany(queryDTO);
            return ResultVO.ok(result);
        } catch (Exception e) {
            log.error("分页查询污水厂预排口手工检测数据失败: {}", e.getMessage(), e);
            return ResultVO.fail("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取污水厂预排口手工检测数据详情", description = "根据ID获取污水厂预排口手工检测数据详情")
    public ResultVO<SewagePlantManualDetectionVO> getDetail(@PathVariable String id) {
        try {
            SewagePlantManualDetectionVO result = sewagePlantManualDetectionService.getDetail(id);
            return ResultVO.ok(result);
        } catch (Exception e) {
            log.error("获取污水厂预排口手工检测数据详情失败: {}", e.getMessage(), e);
            return ResultVO.fail("获取详情失败: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    @Operation(summary = "更新污水厂预排口手工检测数据", description = "更新污水厂预排口手工检测数据")
    public ResultVO<Boolean> updateDetectionData(@RequestBody SewagePlantManualDetectionUpdateListDTO updateDTO) {
        try {
            Boolean result = sewagePlantManualDetectionService.updateDetectionData(updateDTO);
            return ResultVO.ok(result);
        } catch (Exception e) {
            log.error("更新污水厂预排口手工检测数据失败: {}", e.getMessage(), e);
            return ResultVO.fail("更新失败: " + e.getMessage());
        }
    }


    @PostMapping("/export")
    @Operation(summary = "导出污水厂预排口手工检测数据", description = "根据条件导出污水厂预排口手工检测数据为Excel文件")
    public void exportData(@RequestBody SewagePlantManualDetectionQueryDTO queryDTO, HttpServletResponse response) {
        try {
            sewagePlantManualDetectionService.exportData(queryDTO, response);
        } catch (Exception e) {
            log.error("导出污水厂预排口手工检测数据失败: {}", e.getMessage(), e);
            try {
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write("{\"success\":false,\"message\":\"导出失败: " + e.getMessage() + "\"}");
            } catch (IOException ioException) {
                log.error("写入错误响应失败", ioException);
            }
        }
    }
}